package com.zatech.cgnci.project.base.context;

import com.zatech.cgnci.project.base.constant.AuthHeaderConstant;
import com.zatech.cgnci.project.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

/**
 * @author user
 */
@Component
public class UserUtils {

    @Autowired
    private HttpServletRequest requestOirin;
    private static HttpServletRequest request;

    @PostConstruct
    public void init() {
        request = requestOirin;
    }

    // 获取当前用户信息
    public static UserEntity getCurrentUserInfo() {
        String token = request.getHeader(AuthHeaderConstant.auth_request_token_header);
        return UserEntity.USER_ENTITY_MAP.get(token);
    }


    /**
     * 用户退出登录
     */
    public static void currentUserLogout() {
        String token = request.getHeader(AuthHeaderConstant.auth_request_token_header);
        if (StringUtils.isNotBlank(token)) {
            UserEntity.USER_ENTITY_MAP.remove(token);
        }
    }


}
